package haizhi.fruitmall.action;

import com.github.pagehelper.PageInfo;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsCommentForm;
import haizhi.fruitmall.pojo.view.ResultView;
import haizhi.fruitmall.service.GoodsCommentService;
import haizhi.fruitmall.util.BeanUtil;
import haizhi.fruitmall.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Date: 2017/12/25 9:36
 * @Author: Richard Zane
 */

@Slf4j
@Controller
@RequestMapping("/goodsComment")
public class GoodsCommentController {

    @Autowired
    private GoodsCommentService service;

    @RequestMapping(value="/insert",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultView insert(@RequestBody GoodsCommentForm form){
        try {
            service.insert(form);
            log.info("【商品单件评论】插入成功：{}",form);
            return ResultUtil.getSuccess();
        } catch (ResultException e) {
            log.error("【商品单件评论】插入失败：{}",e.getMessage());
            return ResultUtil.getFailure(e);
        }
    }

    @RequestMapping(value="/update",method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultView update(@RequestBody GoodsCommentForm form){
        try {
            service.update(form);
            log.info("【商品单件评论】更新成功：{}",form);
            return ResultUtil.getSuccess();
        } catch (ResultException e) {
            log.error("【商品单件评论】更新失败：{}",e.getMessage());
            return ResultUtil.getFailure(e);
        }
    }

    @RequestMapping(value="/delete",method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultView delete(@RequestParam Long id){
        try {
            service.delete(id);
            log.info("【商品单件评论】删除成功：{}",id);
            return ResultUtil.getSuccess();
        } catch (ResultException e) {
            log.error("【商品单件评论】删除失败：{}",e.getMessage());
            return ResultUtil.getFailure(e);
        }
    }

    @RequestMapping(value="/getone",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultView getone(@RequestParam Long id){
        try {
            return ResultUtil.getSuccess(service.getone(id));
        } catch (ResultException e) {
            return ResultUtil.getFailure(e);
        }
    }

    @RequestMapping(value = "/getall",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResultView getall(GoodsCommentForm form){
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            return ResultUtil.getSuccess(new PageInfo(service.getall(form)));
        }else{
            return ResultUtil.getSuccess(service.getall(form));
        }
    }
}
